﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 设计模式之禅
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //单例模式
            var s = Singleton.GetSingleton();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //简单工厂模式
            var p1 = 工厂模式.简单工厂模式.ConcreteCreator.FactoryMethod("A");
            MessageBox.Show(p1.Say());


            var p2 = 工厂模式.简单工厂模式.ConcreteCreator.FactoryMethod("B");
            MessageBox.Show(p2.Say());

        }

        private void button3_Click(object sender, EventArgs e)
        {
            //工厂方法模式
            var p1 = (new 工厂模式.工厂方法模式.ConcreteCreatorA()).FactoryMethod();
            MessageBox.Show(p1.Say());

            var p2 = (new 工厂模式.工厂方法模式.ConcreteCreatorB()).FactoryMethod();
            MessageBox.Show(p2.Say());
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //抽象工厂模式
            var p1 = (new 抽象工厂模式.Product1Creator());
            MessageBox.Show(p1.ProductAFactoryMethod().Say());
            MessageBox.Show(p1.ProductBFactoryMethod().Say());


            var p2 = (new 抽象工厂模式.Product2Creator());
            MessageBox.Show(p2.ProductAFactoryMethod().Say());
            MessageBox.Show(p2.ProductBFactoryMethod().Say());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //模板方法模式
            var r1 = (new 模板方法模式.ProductA()).Rest();
            MessageBox.Show(r1);

            var r2 = (new 模板方法模式.ProductB()).Rest();
            MessageBox.Show(r2);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var d = new 建造者模式.Director();

            var b1 = new 建造者模式.ConcreteBuilderA();
            var r = d.Construct(b1).Say();
            MessageBox.Show(r.ToString());


            var b2 = new 建造者模式.ConcreteBuilderB();
            var r2 = d.Construct(b2).Say();
            MessageBox.Show(r2.ToString());
        }

        private void button8_Click(object sender, EventArgs e)
        {
            var p = new 代理者模式.ProductProxy();
            MessageBox.Show(p.Say());
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var p = new 原型模式.Product();
            var r = p.Clone();
            MessageBox.Show(r.Say());
        }

        private void button10_Click(object sender, EventArgs e)
        {
            中介者模式.AbstractColleague colleagueA = new 中介者模式.ColleagueA();
            中介者模式.AbstractColleague colleagueB = new 中介者模式.ColleagueB();

            colleagueA.MoneyCount = 100;
            colleagueB.MoneyCount = 100;

            中介者模式.AbstractMediator mediator = new 中介者模式.ConcreteMediator(colleagueA, colleagueB);
            mediator.AWin(10);
            mediator.BWin(5);

            MessageBox.Show(colleagueA.MoneyCount.ToString());
            MessageBox.Show(colleagueB.MoneyCount.ToString());

        }

        private void button11_Click(object sender, EventArgs e)
        {
            命令模式.Invoker invoker = new 命令模式.Invoker();

            命令模式.AbstractReceiver receiverA = new 命令模式.ReceiverA();

            命令模式.AbstractCommand commandA = new 命令模式.CommandA(receiverA);

            invoker.SetCommand(commandA);

            var result = invoker.Action();

            MessageBox.Show(result);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            责任链模式.Client c = new 责任链模式.Client();

            Func<int, string> fc = (i) =>
            {
                return i + "的处理结果：" + c.HandlerRequest(i);
            };

            MessageBox.Show(fc(5));
            MessageBox.Show(fc(55));
            MessageBox.Show(fc(555));
            MessageBox.Show(fc(5555));
        }

        private void button13_Click(object sender, EventArgs e)
        {
            装饰模式.AbstractPhone phone = new 装饰模式.ApplePhone();

            phone = new 装饰模式.DecoratorA(phone);
            var result = phone.Say();

            MessageBox.Show(result);

            phone = new 装饰模式.DecoratorB(phone);
            var result2 = phone.Say();

            MessageBox.Show(result2);

        }

        private void button14_Click(object sender, EventArgs e)
        {
            策略模式.abstractStrategy aStrategy = new 策略模式.ConcreteStrategyA();
            策略模式.abstractStrategy bStrategy = new 策略模式.ConcreteStrategyB();


            策略模式.Context context = new 策略模式.Context(aStrategy);
            var result = context.DoSomething();
            MessageBox.Show(result);


            context = new 策略模式.Context(bStrategy);
            result = context.DoSomething();
            MessageBox.Show(result);

        }

        private void button15_Click(object sender, EventArgs e)
        {
            适配器模式.ITarget adapter = new 适配器模式.Adapter();
            var result = adapter.Say();

            MessageBox.Show(result);
        }

        private void button16_Click(object sender, EventArgs e)
        {
            观察者模式.AbstractSubject subject = new 观察者模式.Subject();

            观察者模式.AbstractObservable observableA = new 观察者模式.ObservableA();
            观察者模式.AbstractObservable observableB = new 观察者模式.ObservableB();

            subject.AddObservable(observableA);
            subject.AddObservable(observableB);

            subject.Say();

        }

        private void button18_Click(object sender, EventArgs e)
        {
            备忘录模式.Originator originator = new 备忘录模式.Originator();
            备忘录模式.Caretaker caretaker = new 备忘录模式.Caretaker();

            originator.SetState("123");
            caretaker.SetState(originator.CreateMemento());
            MessageBox.Show(originator.GetState());


            originator.SetState("456");
            MessageBox.Show(originator.GetState());

            originator.RestoreMemento(caretaker.GetMemento());
            MessageBox.Show(originator.GetState());

        }

        private void button17_Click(object sender, EventArgs e)
        {
            //就是封装一个对外统一调用的类
        }

        private void button19_Click(object sender, EventArgs e)
        {
            访问者模式.ObjectStructure o = new 访问者模式.ObjectStructure();
            foreach (var item in o.Elements)
            {
                MessageBox.Show(item.Accept(new 访问者模式.ConcreteVisitor()));
            }
        }

    }
}
